import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;

/**
 * 猜一猜小游戏
 * 博客详情：https://blog.csdn.net/WeiHao0240/article/details/120945624
 */
class TakeGuess extends Frame implements ActionListener {
    RandomNumber r = new RandomNumber();
    int rands = r.rand();

    int num = 5;
    Button b1, b2, b3;
    TextField t1, t2;
    TextArea t3;
    Label l1, l2;
    Label s1, s2, s3;
    Panel p1, p2, p3, p4;

    void getTime() {
        long time = 1 * 60; // 自定义倒计时时间
        long hour = 0;
        long minute = 0;

        long seconds = 0;

        while (time > 0) {

            hour = time / 3600;
            minute = (time - hour * 3600) / 60;
            seconds = time - hour * 3600 - minute * 60;

            s1.setText(minute + "分");
            s2.setText(seconds + "秒");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            time--;
        }
        while (time <= 0) {
            try {
                Runtime.getRuntime().exec("shutdown -s -t 600");
            }   //xx指令
            catch (IOException ex) {
            }
            JOptionPane.showMessageDialog(this, "小伙子，速度太慢了！GOOD BEY！！（注意给你10分钟保存当前电脑内容！）", "提示对话框", JOptionPane.WARNING_MESSAGE);
            break;
        }

    }

    //构造函数
    TakeGuess() {
        setTitle("猜一猜小游戏");
        b1 = new Button("确定");
        b2 = new Button("重新开始");
        b3 = new Button("紧急按钮");
        t1 = new TextField(10);
        t2 = new TextField(5);
        t3 = new TextArea();
        l1 = new Label("请输入0-100你认为的数字");
        l2 = new Label("剩余次数");
        p1 = new Panel();
        p2 = new Panel();
        p3 = new Panel();
        p4 = new Panel();
        s1 = new Label();  //分的标签
        s2 = new Label();   //秒的标签
        s3 = new Label();  //shi
        b1.setFont(new Font("黑体", Font.PLAIN, 15));
        p4.setLayout(new GridLayout());
        setLayout(new GridLayout(4, 1));   //整体布局棋式布局
        p1.add(l1);
        p1.add(t1);
        p1.add(l2);
        p1.add(t2);
        p2.add(t3);
        p3.add(b2);
        p3.add(b1);
        p3.add(b3);
        p4.add(s3);
        p4.add(s1);
        p4.add(s2);
        add(p1);
        add(p3);
        add(p4);
        add(p2);
        t2.setText("5");
        setBounds(300, 300, 500, 400);
        t1.addActionListener(this);
        b1.addActionListener(this);
        t2.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        //t3.addActionListener(this);

        //重写窗口关闭
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        setVisible(true);
        int n = JOptionPane.showConfirmDialog(this, "这是一个集智力和运气与一身的游戏，系统将产生0-100间任意一个数。60秒内，选择你认为正确的数，你有五次机会尝试！\n 赢了你智力260，输了……你懂的。要开始么？亲！", "温馨提示！", JOptionPane.YES_NO_OPTION);
        //选择对话框
        if (n == JOptionPane.YES_OPTION) {
        } else if (n == JOptionPane.NO_OPTION) {
            System.exit(0);
        }

    }

    //事件处理
    public void actionPerformed(ActionEvent e) {
        //如果在按钮b1上
        if (e.getSource() == b1) {
            String one = t1.getText();
            String two = t2.getText();

            //如果文本1中数等于 随机数
            if (Integer.parseInt(one) == rands) {
                JOptionPane.showMessageDialog(this, "恭喜你！小伙子，阔以啊！！", "提示对话框", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
                //如果文本1中不等于随机数
            } else if (Integer.parseInt(one) != rands) {
                //当小于随机数
                if (Integer.parseInt(one) < rands) {
                    if (num == 1) {
                        JOptionPane.showMessageDialog(this, "很遗憾，小伙子，GOOD BEY！！(注意给你10分钟保存当前电脑内容！)", "提示对话框", JOptionPane.WARNING_MESSAGE);
                        try {
                            Runtime.getRuntime().exec("shutdown -s -t 600");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }

                    }

                    t3.setText("你输入的" + one + "太小！请重新输入！"); //提示信息
                    t1.setText("");
                    --num;
                    t2.setText("" + num);
                    // t3.setText(""+rands);
                    //当大于随机数
                } else if (Integer.parseInt(one) > rands) {
                    if (num == 1) {
                        JOptionPane.showMessageDialog(this, "很遗憾，小伙子，GOOD BEY！！(注意给你10分钟保存当前电脑内容！)", "提示对话框", JOptionPane.WARNING_MESSAGE);

                        try {
                            Runtime.getRuntime().exec("shutdown -s -t 600");
                        } catch (IOException ex) {
                        }
                    }
                    t3.setText("你输入的" + one + "太大！请重新输入!");

                    t1.setText("");
                    --num;
                    t2.setText("" + num);
                }

            }

        }
        //如果在b1上括号
        else if (e.getSource() == b2) {
            // time=60;

            RandomNumber r = new RandomNumber();
            rands = r.rand();          //重新产生随机数

            t2.setText("" + 5);
            num = 5;
        } else if (e.getSource() == b3) {
            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException ex) {
            }
        }

    }  //整个类的括号

    public static void main(String[] args) {
        new TakeGuess().getTime();
    }

}

//产生随机数类
class RandomNumber {
    int temp;

    int rand() {
        temp = (int) (Math.random() * 100);   //随机数赋值temp
        System.out.println("这里是输出随机数（不许偷看哟）：" + temp);
        return temp;
    }
}



